<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Pick-up Master</title>

<link href="styles.css" type="text/css" rel="stylesheet"/>
</head>
    
<body>

<?php include("db.php");

session_start();
check_inactive();
authenticate_player(); 

$db = pg_connect('host=dbsrv1 dbname=csc309g7 user=csc309g7 password=aiboid4p');

$id = $_SESSION['userid'];

$fieldchecked = false;

	
if(!empty($_POST['searchfields']))
{
	foreach($_POST['searchfields']	as $check)
	{
		if ($check == 'username')
		{
			$usercheck = "checked";
		}		
		
		if ($check == 'email')
		{			
			$emailcheck = "checked";
		}		
		
		if ($check == 'location')
		{			
			$locationcheck = "checked";
		}
		
	}
	
	$user_search_query = "select * from people p left join location l on p.lid = l.lid ";	
}
else if($_POST['init_load'])
{
	echo "
	
	<script type='text/javascript'>
	
	
	
	alert('Please check at least one of the fields to begin search!');
	</script>
	
	";
	
}
	
if($db)
{
	if($_POST['searchterm'])
	{
		$condition = "";
		$searchterm =  $_POST['searchterm'];
		$or = false;
	
		
		if($usercheck)
		{
			$condition .= " username ilike '%$searchterm%' ";
			$or = true;
			
		}
		if($emailcheck)
		{
			if($or)
				$condition .= " or email ilike '%$searchterm%' ";
			else
				$condition .= " email ilike '%$searchterm%' ";
			
			$or = true;
			
		}
		if($locationcheck)
		{
			
			if($or)
				$condition .= " or location_name ilike '%$searchterm%' ";
			else
				$condition .= " location_name ilike '%$searchterm%' "; 
			
			
			
		}
			
		if($user_search_query)
		{
			$user_search_query .= " where ";
			 
			$user_search_query .= $condition;
			
			$user_search_result = pg_query($user_search_query );
			
		}
		
	}
	else if($_POST['init_load'])
	{
		
		echo "
		<script type='text/javascript'>	
		alert('Please enter a the search term!');
		</script>";
		
	}


?>





<fieldset>
<legend>User search</legend>

<form action="" name="usf" method="post">
	
	<table>
	<tr>
		<td>Search fields: </td>
	<td>
		
	<input type="hidden" value="userSearch.php" name="pagesource" />	
	<input type="hidden" value="init" name="init_load" />
	<input type="checkbox" name="searchfields[]" value="username" <?=$usercheck?> />Username
	<input type="checkbox" name="searchfields[]" value="email" <?=$emailcheck?> />Email
	<input type="checkbox" name="searchfields[]" value="location" <?=$locationcheck?> />Location
	
	</td>
	<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
	
		<td>
		<?php
		
		if($_POST['searchterm'])
		{
			echo "<input type=\"text\" name=\"searchterm\" value=".$_POST['searchterm'] ." />";
		}
		else
		{
			echo "<input type=\"text\" name=\"searchterm\"  />";
		}
		 ?>
				
		
		
		</td>
		<td> <input type="submit" value="Search" /> </td>
	</tr>
	
	
	</table>


</form>



<?php 
if($user_search_result)
{
	
	
	echo "<table>";
	
	
	
	while($user_data = pg_fetch_assoc($user_search_result))
	{
		$userid = $user_data['userid'];
    	$username = $user_data['username'];
		$userlocation = $user_data['location_name'];
		$useremail = $user_data['email'];
		
		echo "<tr>
			<td><a href=\"profile_player.php?pagesource=usersearch&viewuserid=$userid\">$username</a></td>
			<td>$userlocation</td>
			<td>$useremail</td>
		</tr>";
		
	
	}
	
	

		echo "</table>";
	

}


?>




</fieldset>

<?php 

}
?>


    
    

</body>
</html>
